/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hzf.booklibrary.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.hzf.booklibrary.R;
import com.hzf.booklibrary.base.BaseRVActivity;
import com.hzf.booklibrary.bean.Recommend;
import com.hzf.booklibrary.bean.book.Book;
import com.hzf.booklibrary.component.AppComponent;
import com.hzf.booklibrary.component.DaggerBookComponent;
import com.hzf.booklibrary.ui.contract.SearchContract;
import com.hzf.booklibrary.ui.easyadapter.BookListAdapter;
import com.hzf.booklibrary.ui.presenter.SearchPresenter;
import com.hzf.booklibrary.utils.InputUtils;
import com.hzf.booklibrary.utils.ToastUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/8/6.
 */
public class SearchActivity extends BaseRVActivity<Book> implements SearchContract.View {

    @Bind(R.id.et_search_input)
    EditText etInput;
    @Bind(R.id.rootLayout)
    LinearLayout mRootLayout;
    @Inject
    SearchPresenter mPresenter;

    private int  pageCount =1;

    @Override
    public int getLayoutId() {return R.layout.activity_search;}

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerBookComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void initTitleBar() {}

    @Override
    public void initDatas() {}

    @Override
    public void configViews() {
        hideStatusBar();
        initAdapter(BookListAdapter.class, false, false);
        BookListAdapter.setRecyclerView(mRecyclerView);
        if(mRecyclerView!=null){
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.removeAllItemDecoration();
            mRecyclerView.setItemDecoration(R.drawable.img_search_list_divider,
                    getResources().getDimensionPixelOffset(R.dimen.dimen_1dp),0,0,this);
        }
        mPresenter.attachView(this);
        mRecyclerView.showRecycler();//防止首次进入程序显示Progress
    }


    @OnClick(R.id.btn_left_back)
    public void onClickBack(){
        finish();
    }
    @OnClick(R.id.btn_right_search)
    public void onClickRight(){
        if("".equals(etInput.getText().toString())){
            ToastUtils.showSingleLongToast("请输入搜索内容");
            return;}
        pageCount=1;
        mAdapter.clear();
        mRecyclerView.showProgress();
        mPresenter.getSearchResultList(pageCount+"",etInput.getText().toString());
        if(InputUtils.isInputMethodOpened(SearchActivity.this)){
            InputUtils.hidenInputMethod(mRootLayout,SearchActivity.this);
        }
    }
    @Override
    public void showSearchResultList(List<Book> list) {
        mRecyclerView.showRecycler();
        mAdapter.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int position) {
        BookDetailActivity.startActivity(SearchActivity.this,mAdapter.getItem(position));
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        pageCount++;
        mPresenter.getSearchResultList(pageCount+"",etInput.getText().toString());
    }

    @Override
    public void showError() {
        loaddingError();
        dismissDialog();
    }

    @Override
    public void complete() {
        mRecyclerView.setRefreshing(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}
